package delayQueue

import java.util.concurrent.TimeUnit

import org.redisson.api.{RBlockingDeque, RedissonClient}


/**
  * 延时job执行器
  **/
trait ExecuteDelayJob {

  val delayQueueName: String
  val client: RedissonClient

  def startJobTimer(): Unit = {
    val blockingDeque: RBlockingDeque[DelayJob] = client.getBlockingDeque(delayQueueName)
    // 初始化延迟队列,执行构造方法
    client.getDelayedQueue(blockingDeque)

    new Thread() {
      override def run(): Unit = {
        while (true)
          try {
            val job: DelayJob = blockingDeque.take
            execute(job)
          } catch {
            case e: Exception =>
              e.printStackTrace()
              try
                TimeUnit.SECONDS.sleep(60)
              catch {
                case ex: Exception =>
              }
          }
      }
    }.start()
  }


  def execute(job: DelayJob): Unit
}
